﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleJson;
using com.mogu.util;
namespace com.liyu.pluginframe.util
{
    public class MainDataTool
    {
        private const String DATA = "data";
        public const String START_GAME = "start_game";
        public const String END_GAME = "end_game";
        public const String UPLOAD_GAME_POINT = "upload_game_point";
        public const String UPLOAD_END_GAME_POINT = "upload_end_game_point";
        public const String PUSH_GAME_DATA = "push_game_data";
        public const String PUSH_PROPERTY_DATA = "push_property_data";
        public const String SEND_CHAT = "send_chat";
        public const String LET_USERNAME_OUT = "let_username_out";
        public const String IN_ROOM = "in_room";
        public const String OUT_ROOM = "out_room";
        public const String QUERY_MY_PROP = "query_my_prop";

        private static Dictionary<String, long> api_timeout = new Dictionary<String, long>();
        //private static DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//默认应用中的时间格式化

        private static bool debug = false;


        private static String appcode = null;
        private static String appcode_dt_id = null;
        private static String appname = null;
        private static String static_point = null;
        private static long timeline = 0;


        private static long version = 0;
        private static String roomid = null;
        //    private static String gameroomurl =null;
        private static Dictionary<String, int> userlist = new Dictionary<String, int>();
        private static Dictionary<String, String> nicklist = new Dictionary<String, String>();

        //    private static Map<Integer,String> shunxulist=new HashMap<Integer, String>();
        private static Dictionary<int, Dictionary<String, int>> weizhilist = new Dictionary<int, Dictionary<string, int>>();

        //    private static Context con;



        public static void setIGameSync(IGameSync iGameSync1)
        {
            GameService.getInstance().setiGameSync(iGameSync1);
        }

        /**
         * 获取用户信息
         * @return
         */
        public static UserInfo getUserInfo()
        {
            return userInfo;
        }
        private static UserInfo userInfo = new UserInfo();
     

        /**
         * 获取用户信息、包括房间信息
         * @param   result 句柄
         */
        public static void getUserInfoJSON(String result)
        {
            //        con = mainactivity.getApplicationContext();
            //        appcode = con.getPackageName();
            //        appname =  con.getPackageManager().getApplicationLabel(con.getApplicationInfo()).ToString();
            JsonObject j = null;
            //        String result = mainactivity.getIntent().getExtras().getString("user");
            if (result != null)
            {

                j = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(result);

            }
            if (j != null)
            {
                userInfo.Username = (string)j.getValue("username", null);
                userInfo.Nickname = (string)(j.getValue("nickname", "蘑菇玩家"));
                userInfo.Jid = (string)(j.getValue("jid", null));
                userInfo.Newroom = (bool)(j.getValue("newroom", false));
                userInfo.Challengr = (bool)(j.getValue("challenger", false));
                appcode_dt_id = (string)j.getValue("appcode_dt_id");
                appcode = (string)j.getValue("appcode", null);
                appname = "测试游戏";
                debug = (bool)j.getValue("debug", false);
                debug = false;
                object tem = j.getValue("version", 1);
                if (j.getValue("version", 1).Equals(2L))
                {
                    version = (long)j.getValue("version");
                    roomid = (string)j.getValue("spaceid", "");
                }
                JsonObject json = new JsonObject();
                json["appcode"] = appcode;
                json["appcode_dt_id"] = appcode_dt_id;
                json["username"] = userInfo.Username;
                json["roomid"] = roomid;
                GameService.getInstance().init_room_PomeloClient(j.optString("game_host"), j.optInt("game_point"), j.optString("token"), json);
            }

        }


        private static int syncGameRoom(String route, String json)
        {
            long currentTimeMillis = Tools.GetTimeMillis();
            if (api_timeout.ContainsKey(route))
            {
                if (currentTimeMillis - api_timeout[route] < 100)
                {
                    return 1;
                }
            }
            api_timeout[route] = currentTimeMillis;

            GameService.getInstance().syncGameRoom(appcode, userInfo.Username, roomid, route, json);
            return 0;

        }

        /**
         * 开始游戏
         * @return 0成功，1间隔太短，2失败
         */
        public static void startGame()
        {
            GameService.getInstance().game_round_start_by_room(appcode, roomid);
            //        return syncGameRoom(START_GAME, "{}");
        }

        /**
         * 上传积分，进度……游戏数据
         * @param point
         *  @return 0成功，1间隔太短，2失败
         */
        public static void uploadGamePoint(String point)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["point"] = point;

            GameService.getInstance().upload_point(appcode, roomid, point);
            //        return syncGameRoom(UPLOAD_GAME_POINT, jsonObject.ToString());
        }

        /**
         * 上传最终结果数据
         * @param point
         *  @return 0成功，1间隔太短，2失败
         */
        public static int uploadEndGamePoint(String point)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["point"] = point;

            GameService.getInstance().upload_end_point(appcode, roomid, point);
            return 0;
        }

        /**
         * 向用户发送自定义游戏数据
         * @param jsonObject
         * @param user
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushGameDataToUser(JsonObject jsonObject, String user)
        {

            JsonArray users = new JsonArray();
            users.Add(user);
            jsonObject["users"] = users;
            return syncGameRoom(PUSH_GAME_DATA, jsonObject.ToString());
        }

        /**
         * 向多个用户发送自定义游戏数据
         * @param jsonObject
         * @param users
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushGameDataToUsers(JsonObject jsonObject, String[] users)
        {

            JsonArray jsonArray = new JsonArray();

            foreach (string user in users)
            {
                jsonArray.Add(user);
            }
            jsonObject["users"] = jsonArray;

            return syncGameRoom(PUSH_GAME_DATA, jsonObject.ToString());
        }

        /**
         * 向所有用户发送自定义游戏数据
         * @param jsonObject
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushGameDataToAllUser(JsonObject jsonObject)
        {

            return syncGameRoom(PUSH_GAME_DATA, jsonObject.ToString());
        }

        /**
         * 向用户发送道具数据
         * @param property_flag
         * @param user
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushPropertyDataToUser(String property_flag, String user)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["property_flag"] = property_flag;
            JsonArray jsonArray = new JsonArray();
            jsonArray.Add(user);
            jsonObject["users"] = jsonArray;


            return syncGameRoom(PUSH_PROPERTY_DATA, jsonObject.ToString());
        }

        /**
         * 向多个用户发送道具数据
         * @param property_flag
         * @param users
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushPropertyDataToUsers(String property_flag, String[] users)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["property_flag"] = property_flag;
            JsonArray jsonArray = new JsonArray();
            foreach (String user in users)
            {
                jsonArray.Add(user);
            }
            jsonObject["users"] = jsonArray;

            return syncGameRoom(PUSH_PROPERTY_DATA, jsonObject.ToString());
        }

        /**
         * 向所有用户发送道具数据
         * @param property_flag
         *  @return 0成功，1间隔太短，2失败
         */
        public static int pushPropertyDataToAllUser(String property_flag)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["property_flag"] = property_flag;

            return syncGameRoom(PUSH_PROPERTY_DATA, jsonObject.ToString());
        }

        /**
         * 发送私聊内容
         * @param msg
         * @param user
         *  @return 0成功，1间隔太短，2失败
         */
        public static int sendChatToUser(String msg, String user)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["msg"] = msg;
            jsonObject["user"] = user;

            return syncGameRoom(SEND_CHAT, jsonObject.ToString());
        }

        /**
         *发送谈话内容
         * @param msg 谈话内容
         *  @return 0成功，1间隔太短，2失败
         */
        public static int sendChatToAllUser(String msg)
        {
            JsonObject jsonObject = new JsonObject();

            jsonObject["msg"] = msg;

            return syncGameRoom(SEND_CHAT, jsonObject.ToString());
        }

        /**
         *踢人
         * @param user 被踢的人
         *
         */
        public static void quiteRoom(String user)
        {
            GameService.getInstance().quite_room(user);
        }

        /**
         * 获取当前用户列表，username列表
         * @return 用户列表
         */
        public static List<String> getMembers()
        {
            return GameService.getInstance().get_users();
        }


        /**
         * 退出当前房间，如果在游戏，则退出游戏
         */
        public static void quiteRoom()
        {
            GameService.getInstance().quite_room();
        }

        /**
         * 判断自己是否房主
         * @return
         */
        public static bool isMaster()
        {
            if (GameService.getInstance().get_users().Count == 0)
            {

                return false;
            }
            if (GameService.getInstance().get_users()[0].Equals(getUserInfo().Username))
            {

                return true;

            }
            return false;
        }

        /**
         * 发出请求查询自己的道具
         * @return
         */
        public static void query_my_prop()
        {
            GameService.getInstance().get_user_prop_by_appcode_username(appcode);
        }

        /**
         * 增加道具
         * @return
         */
        public static void add_game_prop(String prop_flag, int num)
        {
            GameService.getInstance().add_prop_by_appcode_username(appcode, prop_flag, num);
        }
        /**
         * 消耗道具
         * @return
         */
        public static void used_game_prop(String prop_flag, int num)
        {
            GameService.getInstance().used_prop_by_appcode_username(appcode, prop_flag, num);
        }

        /**
         * 保存我的游戏信息
         * @param json
         */
        public static void save_game_info_by_appcode_username(String json)
        {
            GameService.getInstance().save_game_info_by_appcode_username(appcode, json);
        }

        /**
         * 获取我的游戏信息
         */
        public static void get_game_info_by_appcode_username()
        {
            GameService.getInstance().get_game_info_by_appcode_username(appcode);
        }

        /**
         * 清空房间内积分信息
         */
        public static void clean_point()
        {
            GameService.getInstance().clean_point(appcode, roomid);
        }
    }
}
